<?php

class action
{
    /**
     * 取消订单
     * @throws AnException
     */
    public function DEALER_ACTION_CANCEL() {

        $order_id = Request::Post('order_id', 0);
        $extra = trim(Request::Post('extra', ''));

        if (empty($extra)) {
            return_data(1, '请填写取消理由');
        }

        call_user_func_array('return_data', order_helper::DEALER_ACTION_CANCEL($order_id, $extra));
    }


    /**
     * 确认扣款
     * @throws AnException
     */
    public function DEALER_ACTION_MINUS() {

        $order_id = Request::Post('order_id', 0);

        if (!$order_id) {
            return_data(1, '无效的订单');
        }

        $order_info = _uri('order', $order_id);

        if (!$order_info ||
            ($order_info['progress'] != order_config::DEALER_WAIT_PAY &&
             $order_info['progress'] != order_config::DEALER_WAIT_PAY_OLD)
        ) {
            return_data(1, '现在还不能做这样的操作');
        }

        $user_info = _uri('user', $order_info['user_id']);

        $price = intval(round($order_info['price'] * 100));
        $paied = intval(round($order_info['pay_user_money'] * 100));
        $need_to_pay = $price - $paied;
        $money = intval(round($user_info['user_money'] * 100));

        if ($money < $need_to_pay) {
            return_data(2, "用户余额不足, 请充值");
        }

        $admin_id = user_helper::get_user_id();

        $need_to_pay *= 0.01;
        $price *= 0.01;

        $info = array(
            'pay_user_money' => $price,
            'progress' => order_config::DEALER_WAIT_CONFIRM,
            'is_pay' => 1,
            'pay_time' => datetime(),
            'confirm_money_user_id' => $admin_id,
            'confirm_money' => $need_to_pay,
            'confirm_money_time' => datetime(),
        );

        $ret = _model('order')->update(array('id' => $order_info['id']), $info);

        if ($ret) {

            // 扣余额
            user_money_helper::change_user_money(array(
                'user_id' => $user_info['id'],
                'user_money' => -1 * $need_to_pay,
                'order_id' => $order_info['id'],
                'admin_id' => $admin_id,
                'res_id' => user_money_config::RES_TYPE_MINUS,
            ));

            // 创建记录
            order_helper::record_dealer_order_operation($order_info['id'], order_config::DEALER_WAIT_CONFIRM, order_config::DEALER_ACTION_MINUS);

            return_data(0, '扣款成功');
        }

        return_data(1, '操作失败, 请稍后重试');
    }

    /**
     * 添加备注(赠品)
     * @throws AnException
     */
    public function DEALER_ACTION_ADD_GOODS() {

        $order_id = Request::Post('order_id', 0);
        $note = Request::Post('note', '');

        if (!$order_id) {
            return_data(1, '无效的订单');
        }

        $order_info = _uri('order', $order_id);

        if (!$order_info || $order_info['progress'] != order_config::DEALER_WAIT_CONFIRM) {
            return_data(1, '现在还不能做这样的操作');
        }

        $note = trim($note);

        if (!$note) {
            return_data(2, '备注不可为空');
        }

        $order_info = _uri('order', $order_id);
        $origin_note = $order_info['note'];

        $info = array(
            'note' => $note,
        );

        $ret = _model('order')->update(array('id' => $order_info['id']), $info);

        if ($ret) {
            // 创建记录
            order_helper::record_dealer_order_operation($order_info['id'], order_config::DEALER_WAIT_CONFIRM, order_config::DEALER_ACTION_ADD_GOODS, json_encode(array(
                $origin_note,
                $note
            )));
            return_data(0, '添加成功');
        }

        return_data(1, '操作失败, 请稍后重试');
    }

    /**
     * 确认订单
     * @throws AnException
     */
    public function DEALER_ACTION_CONFIRM() {
        $order_id = Request::Post('order_id', 0);

        if (!$order_id) {
            return_data(1, '无效的订单');
        }

        $order_info = _uri('order', $order_id);

        if (!$order_info || $order_info['progress'] != order_config::DEALER_WAIT_CONFIRM) {
            return_data(1, '现在还不能做这样的操作');
        }

        $info = array(
            'progress' => order_config::DEALER_WAIT_PRINT,
            'confirm_order_user_id' => user_helper::get_user_id(),
            'confirm_order_time' => datetime(),
        );

        $ret = _model('order')->update(array('id' => $order_info['id']), $info);

        if ($ret) {
            // 创建记录
            order_helper::record_dealer_order_operation($order_info['id'], order_config::DEALER_WAIT_PRINT, order_config::DEALER_ACTION_CONFIRM);
            return_data(0, '订单已确认');
        }

        return_data(1, '操作失败, 请稍后重试');
    }

    /**
     * 打印订单(暂时无用)
     * @throws AnException
     */
    public function DEALER_ACTION_PRINT() {

        // 暂时无效, 直接使用打印页面来设置状态

        return_data(1, '操作失败, 请稍后重试');

        $order_id = Request::Post('order_id', 0);

        if (!$order_id) {
            return_data(1, '无效的订单');
        }

        $order_info = _uri('order', $order_id);

        if ($order_info['progress'] < order_config::DEALER_WAIT_PRINT) {
            return_data(2, '订单无法打印, 请先确认订单');
        }

        if ($order_info['progress'] == order_config::DEALER_WAIT_PRINT) {
            $info = array(
                'progress' => order_config::DEALER_WAIT_SEND,
            );

            $ret = _model('order')->update(array('id' => $order_info['id']), $info);

            if ($ret) {
                // 创建记录
                order_helper::record_dealer_order_operation($order_info['id'], order_config::DEALER_WAIT_SEND, order_config::DEALER_ACTION_PRINT);
                return_data(0, '订单打印成功');
            }
        } else {
            order_helper::record_dealer_order_operation($order_info['id'], $order_info['progress'], order_config::DEALER_ACTION_REPRINT);
            return_data(0, '订单打印成功');
        }

        return_data(1, '操作失败, 请稍后重试');
    }

    /**
     * 订单发货/更新送货单
     * @throws AnException
     */
    public function DEALER_ACTION_SEND() {

        // TODO 权限待定
        //        $allow_role = array(
        //            user_config::ADMIN,
        //            user_config::ASSISTANT,
        //            user_config::MULTIPLE_DIVISION
        //        );
        //
        //        if (!user_helper::is_allow_action($allow_role)) {
        //            return_data(403, '没有此操作权限');
        //        }

        $order_id = Request::Post('order_id', 0);
        $no = trim(Request::Post('no', ''));
        $name = trim(Request::Post('name', ''));
        $note = trim(Request::Post('note', ''));

        if (!$order_id) {
            return_data(1, '无效的订单');
        }

        $order_info = _uri('order', $order_id);
        if (!$order_info ||
            ($order_info['progress'] != order_config::DEALER_WAIT_SEND &&
             $order_info['progress'] != order_config::DEALER_WAIT_SEND_OLD &&
             $order_info['progress'] != order_config::DEALER_SENDING &&
             $order_info['progress'] != order_config::DEALER_SENDING_OLD)
        ) {
            return_data(1, '现在还不能做这样的操作');
        }

        $info = array();

        if ($order_info['progress'] == order_config::DEALER_WAIT_SEND ||
            $order_info['progress'] == order_config::DEALER_WAIT_SEND_OLD
        ) {
            // 发货
            $info['progress'] = order_config::DEALER_SENDING;
            $info['deliver_time'] = datetime();
            $action = order_config::DEALER_ACTION_SEND;
            $extra = [$name, $no, $note];
            $message = '发货成功';
        } elseif ($order_info['progress'] == order_config::DEALER_SENDING ||
                  $order_info['progress'] == order_config::DEALER_SENDING_OLD
        ) {
            // 修改运单
            $action = order_config::DEALER_ACTION_MODIFY_DELIVERY;
            $extra = [
                $order_info['express_name'],
                $order_info['express_no'],
                $order_info['express_note'],
                $name,
                $no,
                $note,
            ];
            $message = '运单更新成功';
        }

        if ($name) {
            $info['express_name'] = $name;
        }

        if ($no) {
            $info['express_no'] = $no;
        }

        if ($note) {
            $info['express_note'] = $note;
        }

        // 更新状态
        $ret = _model('order')->update(array('id' => $order_info['id']), $info);

        if ($ret) {
            // 创建记录
            order_helper::record_dealer_order_operation($order_info['id'], order_config::DEALER_SENDING, $action, json_encode($extra));

            // 统计流水 待废弃
            // order_helper::save_order_status_log($order_id, 'transport', $info);

            // 如果运单号发生变化, 发送短信给合伙人
            if ($order_info['express_no'] != $no) {
                _widget('dealers.message')->transport_order_message($order_info, $name, $no);
            }

            return_data(0, $message);
        }

        return_data(1, '操作失败, 请稍后重试');
    }

    /**
     * 确认到货
     * @throws AnException
     */
    public function DEALER_ACTION_DONE() {

        $order_id = Request::Post('order_id', 0);
        $extra = trim(Request::Post('extra', ''));

        if (empty($extra)) {
            return_data(3, '请填写确认到货备注');
        }

        call_user_func_array('return_data', order_helper::DEALER_ACTION_DONE($order_id, $extra));
    }
}

?>